import { get } from "../../utils/db"
Page({
    data: {
        types: [
            {
                src: "../../imgs/index_07.jpg",
                typename: "营养菜谱"
            },
            {
                src: "../../imgs/index_09.jpg",
                typename: "儿童菜谱"
            },
        ],
        menuList: [],            //菜谱列表
        page: 1,                    //第几页
        pageSize: 10,            //每页数据
    },
    //页面加载
    onLoad() {
        this.getMenuList()
    },
    
    //页面触底
    async onReachBottom() {
        this.data.page++;       //page=2  skip=(2-1)*10=10   limit=10
        this.getMenuList()

    },
    //读取menu集合中所有的数据
    async getMenuList() {
        let skip = (this.data.page - 1) * this.data.pageSize;   //0
        let limit = this.data.pageSize;
        let result = await get({ collection: "menu", skip: skip, limit: limit }).catch(err => {
            console.log(err)
        })
        this.setData({
            menuList: this.data.menuList.concat(result.data)
        })
    },
    //跳转详情页
    toDetail(e) {
        // console.log(e)
        let id = e.currentTarget.id;      //菜谱id
        wx.navigateTo({
            url: '/pages/detail/detail?id=' + id,
            events:{
                //实现menuList中响应数据views加1
                myInc:(e)=>{

                    let id=e.id;        //菜谱id
                    //通过id找到数组的下标
                    let index=this.data.menuList.findIndex(item=>{
                        return item._id==id
                    })
                    //页面中相应数据累加
                    this.data.menuList[index].views++;
                    //把数据渲染到页面
                    this.setData({
                        menuList:this.data.menuList
                    })


                }
            }
           
        })
    },
    //跳转分类页
  toCate(e){
    // let id=e.id;      //菜谱的id
    let id=e.currentTarget.id;    //菜谱id
    wx.navigateTo({
      url: '/pages/type/type?id='+id,
    })
  },
  //跳列表页
  toList(){
    wx.navigateTo({
      url: '/pages/list/list',
    })

  }
})